﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Microsoft.Forums.ForumsClient.Models;


namespace Microsoft.Forums.ForumsClient.Views
{
    /// <summary>
    ///  Converts the current connection status to either an image or text
    /// </summary>
    public class ConnectionStatusConverter : IMultiValueConverter
    {
        BitmapImage onlineIcon;
        BitmapImage offlineIcon;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionStatusConverter"/> class.
        /// </summary>
        public ConnectionStatusConverter()
        {
            onlineIcon = new BitmapImage(new Uri("Images\\Online.ico", UriKind.RelativeOrAbsolute));
            offlineIcon = new BitmapImage(new Uri("Images\\Offline.ico", UriKind.RelativeOrAbsolute));
        }

        #region IMultiValueConverter Members

        /// <summary>
        /// Based on the parameter string return the image or text corresponding to the programs current
        /// connected state
        /// </summary>
        /// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding"/> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the source binding has no value to provide for conversion.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value.If the method returns null, the valid null value is used.A return value of <see cref="T:System.Windows.DependencyProperty"/>.<see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> if it is available, or else will use the default value.A return value of <see cref="T:System.Windows.Data.Binding"/>.<see cref="F:System.Windows.Data.Binding.DoNothing"/> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> or the default value.
        /// </returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string outputChoice = parameter as string;
            if (outputChoice == null)
                throw new ArgumentException("parameter must not be null and must be a string","parameter");

            bool isConnectedAndAuthenticated = 
                AppDataModel.Instance != null &&
                AppDataModel.Instance.Network.IsConnected &&
                AppDataModel.Instance.CurrentUser.LogOnState == LogOnStatus.Authenticated;

            if (outputChoice.Equals("image", StringComparison.OrdinalIgnoreCase))
            {
                if (isConnectedAndAuthenticated)
                {
                    return onlineIcon;
                }
                else
                {
                    return offlineIcon;
                }
            }
            else if (outputChoice.Equals("text", StringComparison.OrdinalIgnoreCase))
            {
                if (isConnectedAndAuthenticated)
                {
                    return Properties.Resources.LoggedInContent;
                }
                else
                {
                    return Properties.Resources.NotLoggedContent;
                }
            }
            else
            {
                throw new ArgumentException("parameter must be either image or text");
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}